package kubayaxi.video.client.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import kubayaxi.video.client.service.ISearchService;
import kubayaxi.video.mybatis.auto.dao.ChannelMapper;
import kubayaxi.video.mybatis.auto.dao.ClientUserMapper;
import kubayaxi.video.mybatis.auto.dao.FollowRelativeMapper;
import kubayaxi.video.mybatis.auto.model.Channel;
import kubayaxi.video.mybatis.auto.model.ClientUser;
import kubayaxi.video.mybatis.auto.model.FollowRelative;
import kubayaxi.video.mybatis.customize.dao.SearchMyMapper;
import kubayaxi.video.mybatis.customize.model.search.dto.SearchChannelDTO;
import kubayaxi.video.mybatis.customize.model.search.dto.SearchUserDTO;
import kubayaxi.video.mybatis.customize.model.search.dto.SearchVideoDTO;
import kubayaxi.video.mybatis.customize.model.search.vo.SearchUserVO;
import kubayaxi.video.mybatis.customize.model.search.vo.SearchVideoVO;
import kubayaxi.video.service.base.common.enums.DeleteEnum;
import kubayaxi.video.service.base.common.enums.FollowStatusEnum;
import kubayaxi.video.service.base.common.request.BaseRequest;
import kubayaxi.video.service.base.common.treadlocal.UserContent;
import kubayaxi.video.service.base.util.common.Asserts;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SearchServiceImpl implements ISearchService {

    @Resource
    private SearchMyMapper searchMyMapper;
    @Resource
    private ClientUserMapper clientUserMapper;
    @Resource
    private ChannelMapper channelMapper;
    @Resource
    private FollowRelativeMapper followRelativeMapper;

    /**
     * 视频搜索
     */
    @Override
    public List<SearchVideoVO> video(BaseRequest<SearchVideoDTO> dto) {
        SearchVideoDTO data = dto.getData();
        Asserts.isNotBlank(data.getKey(),"搜索key为空");
        return searchMyMapper.video(data);
    }

    /**
     * 用户搜索
     */
    @Override
    public List<SearchUserVO> user(BaseRequest<SearchUserDTO> dto) {
        SearchUserDTO data = dto.getData();
        Long userId = UserContent.get();
        Asserts.isNotBlank(data.getKey(),"搜索key为空");
        List<ClientUser> list = clientUserMapper.selectList(new LambdaQueryWrapper<ClientUser>()
                .like(ClientUser::getNickName,data.getKey())
                .eq(ClientUser::getDeleteStatus, DeleteEnum.NOT_DELETED.getCode())
                .orderByDesc(ClientUser::getCreatedTime));
        return list.stream().map(item -> {
            SearchUserVO searchUserVO = new SearchUserVO();
            searchUserVO.setUId(item.getId());
            searchUserVO.setNickName(item.getNickName());
            searchUserVO.setPortraitUrl(item.getPortraitUrl());
            FollowRelative followRelative = followRelativeMapper.selectOne(new LambdaQueryWrapper<FollowRelative>().eq(FollowRelative::getUId, userId).eq(FollowRelative::getBeuId, item.getId()).eq(FollowRelative::getDeleteStatus, DeleteEnum.NOT_DELETED.getCode()));
            searchUserVO.setFollowStatus(ObjectUtil.isNotNull(followRelative) ? FollowStatusEnum.FOLLOWED.getCode() : FollowStatusEnum.NO_FOLLOW.getCode());
            return searchUserVO;
        }).collect(Collectors.toList());
    }

    /**
     * 频道搜索
     */
    @Override
    public List<Channel> channel(BaseRequest<SearchChannelDTO> dto) {
        SearchChannelDTO data = dto.getData();
        Asserts.isNotBlank(data.getKey(),"搜索key为空");
        List<Channel> channelList = channelMapper.selectList(new LambdaQueryWrapper<Channel>()
                .like(Channel::getTitle, data.getKey())
                .eq(Channel::getDeleteStatus, DeleteEnum.NOT_DELETED.getCode())
                .orderByDesc(Channel::getCreatedTime));
        return channelList;
    }
}
